/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server_multiclient;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Tri
 */
public class Server_multiClient {

    /**
     * @param args the command line arguments
     */
    public static int port = 5000;

    public static void main(String[] args) {
        // TODO code application logic here
        ServerSocket serverSocket;
        try {
            serverSocket = new ServerSocket(port);
            while (true) {
                Socket socket = serverSocket.accept();
                new ConnectionHandler(socket);
            }
        } catch (Exception e) {
        }
    }
}

class ConnectionHandler implements Runnable {

    private Socket socket;

    public ConnectionHandler(Socket socket) {
        this.socket = socket;
        Thread theard = new Thread(this);
        theard.start();
    }

    @Override
    public void run() {
        try {
            System.out.println("Client connect to server forward to connect handler");
            BufferedReader inFormclient = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(
                    socket.getOutputStream());
            String strFromClient, strToClient;
            strFromClient = inFormclient.readLine();
            System.out.println("Form Client : " + strFromClient);
            strToClient = strFromClient.toUpperCase()+'\n';
            outToClient.writeBytes(strToClient);
            System.out.println("To Client : "+strToClient);

        } catch (IOException e) {
            e.printStackTrace();
        }

//        throw new UnsupportedOperationException("Not supported yet.");
    }
}